/**
 * 作者:hanbo
 * 日期:2018/11/2311:38
 * 描述:地图控件
 */
import 'jquery';
import {Point} from "../geometry/Point";

export class MapControl {
    constructor(mapView) {
        this._mapView = mapView;
        this.addZoomControl();
        this.addScaleControl();
        this.addCoordControl();
    }

    /**
     * 添加缩放控件
     */
    addZoomControl() {
        const that=this;
        let zoomControl = $("<div class='zoomControl'></div>");
        let zoomInButton = $("<div class='zoomIn zoomButton'></div>");
        let zoomOutButton = $("<div class='zoomOut zoomButton'></div>");
        zoomControl.append(zoomInButton);
        zoomControl.append(zoomOutButton);
        $("#" + this._mapView._id).append(zoomControl);
        zoomInButton.click(function () {
            that._mapView.gLStateCenter.zoomIn();
        });
        zoomOutButton.click(function () {
            that._mapView.gLStateCenter.zoomOut();
        });
    }

    /**
     * 移除缩放控件
     */
    removeZoomControl() {
        $(`#${this._mapView._id} .zoomControl`).remove();
    }

    /**
     * 添加比例尺控件
     */
    addScaleControl() {
        let scaleControl = $("<div class='scaleControl'></div>");
        let denominator = $("<div class='denominator'></div>");
        let line = $("<div class='line'></div>");
        let molecule = $("<div class='molecule'></div>");
        molecule.text("1cm");
        scaleControl.append(denominator);
        scaleControl.append(line);
        scaleControl.append(molecule);
        $("#" + this._mapView._id).append(scaleControl);
    }

    /**
     * 移除比例尺控件
     */
    removeScaleControl() {
        $(`#${this._mapView._id} .scaleControl`).remove();
    }

    /**
     * 更新比例尺控件
     * @param denominator
     */
    updateScaleControl(denominator) {
        let result = Math.round(denominator);
        let num = result.toString().length;//求整数位数
        switch (num) {
            case 1:
            case 2:
            case 3:
                result = result + "m";
                break;
            case 4:
            case 5:
            case 6:
                result = Math.round(result/1000) + "Km";
                break;
            case 7:
            case 8:
            case 9:
                result = Math.round(result/1000000) + "Mm";
                break;
            default:
                result = "超出范围";
                break;
        }
        $(`#${this._mapView._id} .denominator`).text(result);
    }

    /**
     * 添加坐标显示控件
     */
    addCoordControl(){
        let coordControl = $("<div class='coordControl'>坐标显示</div>");
        $("#" + this._mapView._id).append(coordControl);
        this.updateCoordText();
    }

    /**
     * 移除坐标显示控件
     */
    removeCoordControl(){
        $(`#${this._mapView._id} .coordControl`).remove();
    }

    /**
     * 更新坐标显示文字
     */
    updateCoordText(){
        let center=this._mapView.gLStateCenter.getMapCenter();
        let center_4326=Point.mectorToWGS(center);
        $(".coordControl").html(
            `X:${center.x.toFixed(2)} Y:${center.y.toFixed(2)}<br/>
             B:${center_4326.x.toFixed(2)} L:${center_4326.y.toFixed(2)}`
        );
    }
}